<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2020/10/20
 * Time: 10:39
 */
namespace App\Api\Controllers\Glow;


use App\Models\Merchant;
use App\Models\MerchantStore;
use App\Models\WincodeStore;
use App\Models\Glow;
use App\Models\Store;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class StoreController extends BaseController
{
    //新增激活码
    public function add_code(Request $request)
    {
        try {
            $data = $request->all();
            $merchant_id = $request->post('merchant_id', '');
            $store_id = $request->post('store_id', '');
            $shop_id = $request->post('shop_id', '');
            $soft_uid = $request->post('soft_uid', '');
            $brand_id = $request->post('brand_id', '');
            $soft_version_uid = $request->post('soft_version_uid', '');
            if(!empty($data['store_id'])){
                $store_id = $data['store_id'];
            }
            if(!empty($data['merchant_id'])){
                $merchant_id = $data['merchant_id'];
            }
            $check_data = [
                'merchant_id' => '收银员',
                'store_id' => '门店',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $store = Store::where('store_id', $store_id)
                ->select('store_name', 'id','shop_id','people_phone','people','pid')
                ->first();
            if(!empty($shop_id)){
                Store::where('store_id', $store_id)->update(array('shop_id'=>$shop_id));
            }
            $store = Store::where('store_id', $store_id)
                ->select('store_name', 'id','shop_id','people_phone','people','pid')
                ->first();
            if (!$store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店不存在'
                ]);
            }

            $MerchantStore = MerchantStore::where('store_id', $store_id)
                ->where('merchant_id', $merchant_id)
                ->select('id')
                ->first();
            if (!$MerchantStore) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店&收银员不存在'
                ]);
            }

            $merchant_name = Merchant::where('id', $merchant_id)
                ->select('name')
                ->first()
                ->name;
            if(empty($shop_id) && empty($store->pid)){
                if(empty($store->shop_id)){
                    $shop_data=array('agent_id'=>$this->agent_id,'shop_name'=>$store->store_name,'phone'=>$store->people_phone,'keeper'=>$store->people);
                    $shop_data['sign']=$this->return_data($shop_data);
                    $res=$this->curl($shop_data,'http://internal.congmingpay.com/internal/createnewdevicemerchant.do');
                    $res=json_decode($res,true);
                    
                     error_log(date('Y-m-d H:i:s')." 【添加萤火虫插件】： ".json_encode($res)."\n\n", 3, "/tmp/". 'test'.date('Ymd').".log");
       
                    
                    if(!empty($res['error_msg'])){
                         return json_encode([
                            'status' => 2,
                            'message' => '参数错误，创建聪明付商户失败'
                        ]);
                    }
                    Store::where('store_id', $store_id)->update(array('shop_id'=>$res['shop_id']));
                    $shop_id=$res['shop_id'];
                }else{
                    $shop_id=$store->shop_id;
                }
            }
            if(!empty($store->pid)){
                $store = Store::where('id', $store->pid)
                ->select('store_name', 'id','shop_id','people_phone','people')
                ->first();
                 $shop_id=$store->shop_id;
            }
            if($brand_id=='gww'){
                if(empty($soft_uid) || empty($soft_version_uid)){
                     return json_encode([
                        'status' => 2,
                        'message' => '请选择萤火虫软件及版本'
                    ]);
                }
            }
            
            
            
            
            $pash = $request->path();
            $pass = $request->get('pass', '');
            $pass = substr(base64_decode($pass), 0, -5);
            
            $pass_verify = $this->pass_verify($pass, $pash); // 密钥验证
            $pass_verify = json_decode($pass_verify, true);
            // print_r( $pass_verify );die;
            if( $pass_verify['status'] == '1'){
                $pass_id = DB::table('pass')->where(['pass' => $pass])->value('id'); // 密钥id
                $remark = '门店萤火虫: 门店id: ' . $store_id . '添加门店萤火虫';
                $this->operation_log( $this->parseToken()->user_id, 14, $remark , $pass_id );
            }else{
                return json_encode(['status' => 2, 'message' => '权限不足', 'data' => []]);
            }
            
            
            
            
            
            $array=array('brand_id'=>$brand_id,'shop_id'=>$shop_id);
            if(!empty($soft_uid)){
                $array['soft_uid']=$soft_uid;
            }
            if(!empty($soft_version_uid)){
                $array['soft_version_uid']=$soft_version_uid;
            }
            $array['sign']=$this->return_data($array);
            $res1=$this->curl($array,'http://internal.congmingpay.com/internal/initdevicebind.do');
            $res1=json_decode($res1,true);
            
             error_log(date('Y-m-d H:i:s')." 【添加萤火虫插件】： ".json_encode($res1)."\n\n", 3, "/tmp/". 'test'.date('Ymd').".log");
       
            
            if (empty($res1['result_code']) || $res1['result_code']=='fail') {
                return json_encode([
                    'status' => 2,
                    'message' => '创建失败'
                ]);
            }
            $insert = [
                'store_id' => $store_id,
                'store_name' => $store->store_name,
                'merchant_id' => $merchant_id,
                'merchant_name' => $merchant_name,
                'device_id' => $res1['sn'],
                'activation_code' => $res1['yhc_secret_key'],
                'short_key' => $res1['short_key'],
                'secret_key' => $this->key,
                'agent_id' => $this->agent_id
            ];
            $ress = Glow::create($insert);
            if ($ress) {
                return json_encode([
                    'status' => 1,
                    'message' => '添加成功',
                    'data' => $insert
                ]);
            } else {
                return json_encode([
                    'status' => 1,
                    'message' => '添加成功',
                    'data' => $insert
                ]);
            }
        } catch (\Exception $ex) {
            return json_encode([
                'status' => -1,
                'message' => $ex->getMessage().' | '.$ex->getLine()
            ]);
        }
    }


    //激活码列表
    public function code_list(Request $request)
    {
        try {
            //获取请求参数
            $data = $request->all();

            $check_data = [
                'store_id' => '门店',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            $store = Store::where('store_id', $data['store_id'])
                ->select('store_name')
                ->first();
            if (!$store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店不存在'
                ]);
            }

            $obj = Glow::where('store_id', $data['store_id'])
                ->orderBy('created_at', 'desc');
            $this->t = $obj->count();
            $data = $this->page($obj)->get();
            $this->status = 1;
            $this->message = '数据返回成功';
            return $this->format($data);
        } catch (\Exception $ex) {
            return json_encode([
                'status' => -1,
                'message' => $ex->getMessage().' | '.$ex->getLine()
            ]);
        }
    }


    //删除激活码
    public function del_code(Request $request)
    {
        try {
            //获取请求参数
            $public = $this->parseToken();
            $data = $request->all();
            $device_id = $request->get('device_id');
            $store_id = $request->get('store_id');
            $merchant_id = $request->get('merchant_id');

            $check_data = [
                'merchant_id' => '收银员',
                'store_id' => '门店',
            ];
            $check = $this->check_required($request->except(['token']), $check_data);
            if ($check) {
                return json_encode([
                    'status' => 2,
                    'message' => $check
                ]);
            }

            if (1) {
                return json_encode([
                    'status' => 2,
                    'message' => '没有权限删除'
                ]);
            }

            $store = Store::where('store_id', $data['store_id'])
                ->select('id')
                ->first();
            if (!$store) {
                return json_encode([
                    'status' => 2,
                    'message' => '门店不存在'
                ]);
            }

            $res = Glow::where('device_id', $device_id)
                ->where('store_id', $store_id)
                ->where('merchant_id', $merchant_id)
                ->delete();
            if ($res) {
                return json_encode([
                    'status' => 1,
                    'message' => '删除成功',
                ]);
            } else {
                return json_encode([
                    'status' => 2,
                    'message' => '删除失败',
                ]);
            }
        } catch (\Exception $ex) {
            return json_encode([
                'status' => -1,
                'message' => $ex->getMessage().' | '.$ex->getLine()
            ]);
        }
    }


}
